

import RankMgr, { rankMgrType } from "./RankMgr";
import Home from "./home";
import homeRankItem from "./homeRankItem";
import rankingList from "./rankingList";
import rankingObj from "./rankingObj";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ScrollViewExample extends cc.Component {

    @property(cc.Prefab)
    public itemPrefab: cc.Prefab = null;

    @property(cc.Node)
    public content: cc.Node = null;

    @property(cc.ScrollView)
    public scrollView: cc.ScrollView = null;

    @property({ displayName: "生成物品的高" })
    itemHeight: number = 400;

    @property({ displayName: "初始生成的数量" })
    initCreatorNum: number = 4;

    @property({ displayName: "滑动组件向下的延伸长度" })
    bottom: number = 200;
    private itemsPool: cc.Node[] = [];
    private items: cc.Node[] = []; // 当前内容中的项

    private data: rankMgrType = null; // 这里是您的数据

    private oldPos: cc.Vec3 = null;

    private curTag: number = -1;

    /** 是否移动到顶部 */
    private isMoveTop: boolean = false;

    private managerScript: Home = null;

    onLoad() {
        this.getDataConfig();
        // 基于预制体初始化对象池
        for (let i = 0; i < this.initCreatorNum; ++i) {
            let item = cc.instantiate(this.itemPrefab); // 实例化预制体
            this.content.addChild(item);
            this.itemsPool.push(item); // 加入对象池
            item.active = false; // 初始时隐藏
        }
        this.content.height = (this.itemHeight * this.data.cityRankInfo.length) + this.bottom ;
        // 初始填充
        this.fillContent();
        this.oldPos = this.content.position;
        this.managerScript = this.node.parent.getComponent(Home);
    }

    getDataConfig() {
        let rankMgr = this.node.getComponent(RankMgr);
        let data = rankMgr.getRankInfo();
        this.data = data;
    }

    fillContent() {
        for (let i = 0; i < this.data.cityRankInfo.length && i < this.initCreatorNum; ++i) {
            this.renderItem(i, -this.itemHeight * (0.5 + i) );
        }
    }

    renderItem(index: number, y: number) {
        let item = this.itemsPool.pop(); // 获取一个对象
        if (!item) {
            item = cc.instantiate(this.itemPrefab);
            this.content.addChild(item);
        }
        item.setPosition(new cc.Vec3(0, y, 0));
        item.active = true;
        // 适当地设置 item 数据
        // 例如：item.getComponent('ItemComponent').setData(this.data[index]);
        this.items.push(item);
    }

    /** 根据索引获取当前的y坐标 */
    getCurYPos(tag) {
        return (this.itemHeight * tag);
    }

    update(dt: number) {
        let y = this.content.y - this.oldPos.y;
        let index = this.getCurSuoYinPos();
        if (index == -1 || index >= this.data.cityRankInfo.length - this.initCreatorNum + 1) {
            return;
        }
        if (this.curTag == index) {
            return;
        }
        this.renewContentPos(index);
        this.curTag = index;
    }

    /** 更新显示 */
    renewShow() {
        let index = this.getCurSuoYinPos();
        this.renewContentPos(index);
        this.curTag = index;
    }

    /** 获取当前索引所在位置 */
    getCurSuoYinPos() {
        let y = this.content.y - this.oldPos.y;
        let index = -1;
        for (let i = 0; i < this.data.cityRankInfo.length; i++) {
            if (this.getCurYPos(i) <= y && this.getCurYPos(i + 1) > y) {
                index = i;
                break;
            }
        }
        return index;
    }

    /** 根据当前索引更新content的子节点位置 */
    renewContentPos(tag) {
        // 通过判断他是上移动还是下一来修改对象池里的节点顺序
        if (this.curTag < tag) {
            // 界面 向上移动
            let moveNodeList = this.items.splice(0, 1);
            this.items.push(moveNodeList[0]);
        }
        else {
            // 界面 向下移动
            let moveNode = this.items.pop();
            this.items.unshift(moveNode)
        }

        let rankingScript = null;
        if (this.node.parent.name == "content") {
            rankingScript = this.node.parent.parent.getComponent(rankingList);
            if (rankingScript) {
                this.data.cityRankInfo.sort((a, b) => {
                    if (rankingScript.initSelectMode == 1) {
                        return (b.grade - a.grade);
                    }
                    else if (rankingScript.initSelectMode == 2) {
                        return (b.university - a.university);
                    }
                })
            }
        }
        for (let i = 0; i < this.items.length; i++) {
            let item = this.items[i];
            item.position = cc.v3(0, -this.getCurYPos(i + tag) - (this.itemHeight / 2), 0);
            let comp = item.getComponent(homeRankItem);
            if (comp != null) {
                if (comp) {
                    comp.updateContent(this.data, i + tag); // 设置新的数据
                }
            }
            else {
                let comp2 = item.getComponent(rankingObj);
                if (comp2) {
                    if (rankingScript)
                        comp2.updateContent1(this.data, i + tag, rankingScript.initSelectMode);
                }
            }
        }
        if (this.managerScript != null) {
            if (this.isInMyDiQuPos(tag)) {
                this.managerScript.renewDingWeiShow(true);
            }
            else {
                this.managerScript.renewDingWeiShow(false);
            }
        }
    }

    /** 将视图滚动到指定区域 -- 传标签 */
    moveShiTuToPos(tag) {
        cc.log('定位～！'+tag)
        if (tag == -1) {
            return;
        }
        if (tag >= this.data.cityRankInfo.length - 2) {
            tag = this.data.cityRankInfo.length - 2;
        }
        let posY = ((tag) * this.itemHeight) + this.oldPos.y ;
        let movePos = cc.v3(0, posY, 0)

        if (this.managerScript.isMoveTop) {
            movePos = cc.v3(0, this.oldPos.y, 0);
        }
        this.scrollView.stopAutoScroll();
        cc.tween(this.content)
            .to(0.6, { position: movePos })
            .start();
    }

    /** 是否在自己地区的区域 */
    isInMyDiQuPos(tag): boolean {

        if (tag >= this.data.cityRankInfo.length - 5) {
            return true;
        }
        if ((tag) == this.managerScript.curDingWei) {
            return true;
        }
        else {
            return false;
        }
    }

}
